utils = {
  setParam: function(name, value) {
    localStorage.setItem(name, value);
  },
  getParam: function(name) {
    return localStorage.getItem(name);
  },
  checkExist: function(compare, compared) {
	  if(compare.productNo == compared.productNo && 
			  compare.colorId == compared.colorId &&
			  compare.size == compared.size){
		  return true;
	  }
	  return false;
  },
  checkExitInList: function(detail, details){
	  var exist = false;
	  for(var i in details){
		  if(detail.productNo == details[i].productNo && 
				  detail.colorId == details[i].colorId &&
				  detail.size == details[i].size){
			  exist = true;
		  }
	  }
	  return exist;
  }
};

cart = {
  // 向购物车中添加商品
  addproduct: function(product) {
    var ShoppingCart = utils.getParam("ShoppingCart");
    if (ShoppingCart == null || ShoppingCart == "") {
      // 第一次加入商品
      ShoppingCart = {
        "orderDetails": [product],
        "totalNumber": product.num,
        "totalAmount": (product.price * product.num).toFixed(2)
      };
      utils.setParam("ShoppingCart", JSON.stringify(ShoppingCart));
    } else {
      ShoppingCart = JSON.parse(ShoppingCart);
      var orderDetails = ShoppingCart.orderDetails;
      var isExist = false;
      for (var i in orderDetails) {
        if (utils.checkExist(orderDetails[i], product)) {
        	isExist = true;
        	orderDetails[i].num = parseInt(orderDetails[i].num) + parseInt(product.num);
        }
      }
      if (!isExist) {
        // 没有该商品就直接加进去
        orderDetails.push(product);
      }
      // 重新计算总价
      ShoppingCart.totalNumber = parseInt(ShoppingCart.totalNumber) + parseInt(product.num);
      ShoppingCart.totalAmount = parseFloat(ShoppingCart.totalAmount) + (parseInt(product.num) * parseFloat(product.price));
      // 保存购物车
      utils.setParam("ShoppingCart", JSON.stringify(ShoppingCart));
    }
  },
  //修改商品
  editProd:function(newProd, oldProd){
	  if(utils.checkExist(newProd, oldProd)){
		  if(newProd.num == oldProd.num){
			  return;
		  }
	  }
	  var ShoppingCart = JSON.parse(utils.getParam("ShoppingCart"));
	  var orderDetails = ShoppingCart.orderDetails;
	  for(var i in orderDetails){
		  if(utils.checkExist(orderDetails[i], oldProd)){
			  ShoppingCart.totalNumber = parseInt(ShoppingCart.totalNumber) - parseInt(orderDetails[i].num);
	    	  var totalAmount = parseFloat(ShoppingCart.totalAmount) - parseInt(orderDetails[i].num) * parseFloat(orderDetails[i].price);
	    	  ShoppingCart.totalAmount = totalAmount.toFixed(2);
	    	  
			  ShoppingCart.totalNumber = parseInt(ShoppingCart.totalNumber) + parseInt(newProd.num);
			  totalAmount = parseFloat(ShoppingCart.totalAmount) + (parseInt(newProd.num) * parseFloat(newProd.price));
			  ShoppingCart.totalAmount = totalAmount.toFixed(2);
			  orderDetails[i] = newProd;
			  utils.setParam("ShoppingCart", JSON.stringify(ShoppingCart));
			  return;
		  }
	  }
  },
  // 修改给买商品数量
  updateproductnum: function(product, num) {
    var ShoppingCart = JSON.parse(utils.getParam("ShoppingCart"));
    var orderDetails = ShoppingCart.orderDetails;
    for (var i in orderDetails) {
      if (utils.checkExist(orderDetails[i], product)) {
        var count = orderDetails[i].num + parseInt(num);
        if (count == 0) {
          return;
        }
        ShoppingCart.totalNumber = parseInt(ShoppingCart.totalNumber) + parseInt(num);
        var totalAmount = parseFloat(ShoppingCart.totalAmount) + (parseInt(num) * parseFloat(orderDetails[i].price));
        ShoppingCart.totalAmount = totalAmount.toFixed(2);
        orderDetails[i].num = count;
        utils.setParam("ShoppingCart", JSON.stringify(ShoppingCart));
        return;
      }
    }
  },
  // 获取购物车中的所有商品
  getOrder: function() {
    var ShoppingCart = utils.getParam("ShoppingCart");
    if (ShoppingCart == null) {
      return null;
    }
    return JSON.parse(ShoppingCart);
  },
  // 删除购物车中商品
  deleteproduct: function(product) {
    var ShoppingCart = JSON.parse(utils.getParam("ShoppingCart"));
    var orderDetails = ShoppingCart.orderDetails;
    var list = [];
    for (var i in orderDetails) {
      if (utils.checkExist(orderDetails[i], product)) {
    	  ShoppingCart.totalNumber = parseInt(ShoppingCart.totalNumber) - parseInt(orderDetails[i].num);
    	  var totalAmount = parseFloat(ShoppingCart.totalAmount) - parseInt(orderDetails[i].num) * parseFloat(orderDetails[i].price);
    	  ShoppingCart.totalAmount = totalAmount.toFixed(2);
      } else {
    	  list.push(orderDetails[i]);
      }
    }
    ShoppingCart.orderDetails = list;
    utils.setParam("ShoppingCart", JSON.stringify(ShoppingCart));
  },
  replaceShopcar: function(shopcar) {
    utils.setParam("ShoppingCart", JSON.stringify(shopcar));
  },
  replaceOrderDetails: function(orderDetails) {
	  var ShoppingCart = JSON.parse(utils.getParam("ShoppingCart"));
	  ShoppingCart.orderDetails = orderDetails;
	  utils.setParam("ShoppingCart", JSON.stringify(ShoppingCart));
  },
  clearShopcar: function() {
    localStorage.clear();
  },
  submitOrder: function(orderDetails){
	  var ShoppingCart = JSON.parse(utils.getParam("ShoppingCart"));
	  ShoppingCart.orderDetails = orderDetails;
	  utils.setParam("order", JSON.stringify(ShoppingCart));
  },
  getSubmitOrder: function(){
	  var ShoppingCart = utils.getParam("order");
	  if (ShoppingCart == null) {
	     return null;
	   }
	   return JSON.parse(ShoppingCart);
  },
  replaceSubmitOrder: function(shopcar) {
	    utils.setParam("order", JSON.stringify(shopcar));
	  },
  removeSubmitSuccess: function(order){
	  var ShoppingCart = JSON.parse(utils.getParam("ShoppingCart"));
	  if(ShoppingCart == null){
		  return false;
	  }
	  var shoppingCartDetails = ShoppingCart.orderDetails;
	  var orderDetails = order.orderDetails;
	  var list = [];
	  for (var i in shoppingCartDetails) {
		  if(utils.checkExitInList(shoppingCartDetails[i], orderDetails)){
			  ShoppingCart.totalNumber = parseInt(ShoppingCart.totalNumber) - parseInt(shoppingCartDetails[i].num);
			  var totalAmount = parseFloat(ShoppingCart.totalAmount) - parseInt(shoppingCartDetails[i].num) * parseFloat(shoppingCartDetails[i].price);
			  ShoppingCart.totalAmount = totalAmount.toFixed(2);
		  }else{
			  list.push(shoppingCartDetails[i]);
		  } 
	  }
	  ShoppingCart.orderDetails = list;
	  localStorage.clear();
	  utils.setParam("ShoppingCart", JSON.stringify(ShoppingCart));
  }
};